/*
 * Copyright (C) 2023 Coder.AN
 * Email: an.hongjun@foxmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include "edge_tensor.h"
#include "edge_tensor_kernel.h"
#include <stdio.h>


__global__ void Uint8ToFloat32_kernel(const uint8_t* src, float* dst, size_t unit_count)
{
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx >= unit_count) return;
    float val = src[idx];
    dst[idx] = val;
}

void ConvertUint8ToFloat32(const uint8_t* src, float* dst, size_t unit_count)
{
    dim3 block(1024);
    dim3 grid((unit_count - 1) / block.x + 1);
    Uint8ToFloat32_kernel<<<grid, block>>>(src, dst, unit_count);
    CHECK(cudaDeviceSynchronize());
    return;
}

__global__ void Int32ToUint8_kernel(const int32_t* src, uint8_t* dst, size_t unit_count)
{
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx >= unit_count) return;
    uint8_t val = src[idx];
    dst[idx] = val;
}

void ConvertInt32ToUint8(const int32_t* src, uint8_t* dst, size_t unit_count)
{
    dim3 block(1024);
    dim3 grid((unit_count - 1) / block.x + 1);
    Int32ToUint8_kernel<<<grid, block>>>(src, dst, unit_count);
    CHECK(cudaDeviceSynchronize());
    return;
}
